package com.kstech.exccheck.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;
import android.widget.Toast;

import com.kstech.exccheck.domain.checkline.CheckLineManager;
import com.kstech.exccheck.util.AppContext;
import com.kstech.exccheck.util.DateUtil;
import com.kstech.exccheck.view.upload.Globals;

import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Date;

/**
 * The type Wifi receiver. 已弃用
 */
public class WifiReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
        if(intent.getBooleanExtra("null",false)){
            if(Globals.isSingleChecking && Globals.WIFINotify){
                Log.e("System.Out","socket is null");
                CheckLineManager.connectCheckLine(AppContext.getCurrentCheckLine());
                Globals.WIFINotify = false;
            }
        }
        if(intent.getAction().equals(WifiManager.RSSI_CHANGED_ACTION)){
            //write("wifi signal strength changed");
            //signal strength changed
        }
        else if(intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)){//wifi连接上与否
            Log.e("System.Out","网络状态改变");
            NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
            if(info.getState().equals(NetworkInfo.State.DISCONNECTED)){
                Log.e("System.Out","wifi网络连接断开"+wifiManager.getConnectionInfo().getSSID());
                WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                if(Globals.isSingleChecking && "0x".equals(wifiInfo.getSSID().trim())){
                    Log.e("System.Out","wifi断开"+wifiManager.getConnectionInfo());
                    CheckLineManager.connectCheckLine(AppContext.getCurrentCheckLine());
                }
            }
            else if(info.getState().equals(NetworkInfo.State.CONNECTED)){

                WifiInfo wifiInfo = wifiManager.getConnectionInfo();

                //获取当前wifi名称
                Log.e("System.Out","连接到网络"+wifiInfo.getSSID()+wifiInfo.getBSSID());
                if(Globals.isSingleChecking){
                    if(!"\"Dlink_DWL2000\"".equals(wifiInfo.getSSID()) || !"\"DLINK_DWL2000_01\"".equals(wifiInfo.getSSID())){
                        CheckLineManager.connectCheckLine(AppContext.getCurrentCheckLine());
                        Globals.WIFISTATUS.clear();
                        Log.e("System.Out","成功连接到网络");
                    }
                }
            }

        }
        else if(intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)){//wifi打开与否
            int wifistate = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_DISABLED);

            if(wifistate == WifiManager.WIFI_STATE_DISABLED){
                Log.e("System.Out","系统关闭wifi");
                if(Globals.isSingleChecking){
                    Toast.makeText(AppContext.context,"系统WIFI已关闭，请打开",Toast.LENGTH_SHORT).show();
                }
            }
            else if(wifistate == WifiManager.WIFI_STATE_ENABLED){
                Log.e("System.Out","系统开启wifi");
                Toast.makeText(AppContext.context,"系统WIFI已打开",Toast.LENGTH_SHORT).show();
                if(Globals.isSingleChecking){
                    CheckLineManager.connectCheckLine(AppContext.getCurrentCheckLine());
                }
            }
        }
    }

}
